package org.ndot.java.util.concurrent.bank.ThreadPool;

import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

import org.ndot.java.util.concurrent.bank.BankParam;
import org.ndot.java.util.concurrent.bank.Consumer;

/**
 * @公司名称: 小蚂蚁学堂
 * 
 * @项目名称: J2SE_Study
 * 
 * @类名名称: org.ndot.java.util.concurrent.bank.ThreadPool.BankWin.java
 * 
 * @作者姓名: SunJincheng
 * 
 * @软件版本: v1.0
 * 
 * @创建日期: 2010-4-14上午09:04:36
 */
public class BankWin extends Thread {
	private boolean end = false;
	private ThreadPoolExecutor winPool;

	private List<Consumer>[] consumerList;

	public BankWin(ThreadPoolExecutor winPool, List<Consumer>... consumerList) {
		this.winPool = winPool;
		this.consumerList = consumerList;
	}

	public void run() {
		try {
			while (!end || consumerList[0].size() > 0
					|| (consumerList.length > 1 && consumerList[1].size() > 0)) {
				synchronized (consumerList[0]) {
					if (consumerList[0].size() > 0) {
						Consumer c = consumerList[0].get(0);
						winPool.execute(new Operator(c));
						consumerList[0].remove(c);
					}
				}
				if (consumerList[0].size() == 0 && consumerList.length > 1) {
					synchronized (consumerList[1]) {
						if (consumerList[1].size() > 0) {
							Consumer c = consumerList[1].get(0);
							winPool.execute(new Operator(c));
							consumerList[1].remove(c);
						}
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void setEnd(boolean end) {
		this.end = end;
	}

}
